package com.baseproject.slidingmenu;

import android.app.Activity;
import android.support.v4.widget.SlidingPaneLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by hp on 2017/7/19.
 */

public class SlidingPaneLayoutStrategy implements ISlidingLayout, SlidingPaneLayout.PanelSlideListener {

    MySliddingMenu mySliddingMenu;
    private ISlidingListener mSlidingListener;
    View slidingView;
    Activity mActivity;

    @Override
    public void adjustContentAndSlidingView(Activity activity, Object ... layoutIds) {
        mActivity = activity;
        mySliddingMenu = new MySliddingMenu(activity);
        if (layoutIds[0] instanceof Integer) {
            LayoutInflater.from(activity).inflate((Integer) layoutIds[0], mySliddingMenu, true);
        } else if (layoutIds[0] instanceof View) {
            mySliddingMenu.addView((View) layoutIds[0]);
        }
        slidingView = mySliddingMenu.getChildAt(0);

        ViewGroup contentParent = (ViewGroup) activity.findViewById(android.R.id.content);
        View content = contentParent.getChildAt(0);
        contentParent.removeView(content);
        mySliddingMenu.addView(content);
        contentParent.addView(mySliddingMenu);
    }

    @Override
    public void setContentFadeColor(int colorId) {
        mySliddingMenu.setSliderFadeColor(mActivity.getResources().getColor(colorId));
    }

    @Override
    public boolean isOpen(int type) {
        return mySliddingMenu.isOpen();
    }

    @Override
    public void open(int type) {
        mySliddingMenu.openPane();
    }

    @Override
    public void close(int type) {
        mySliddingMenu.closePane();
    }

    @Override
    public void addSlidingListener(ISlidingListener slidingListener) {
        if (null != slidingListener) {
            mSlidingListener = slidingListener;
            mySliddingMenu.setPanelSlideListener(this);
        }
    }

    @Override
    public View getSlidingLayout(int type) {
        return slidingView;
    }

    @Override
    public void onPanelSlide(View panel, float slideOffset) {
        mSlidingListener.onSlidingSlide(panel, slideOffset);
    }

    @Override
    public void onPanelOpened(View panel) {
        mSlidingListener.onSlidingOpened(panel);
    }

    @Override
    public void onPanelClosed(View panel) {
        mSlidingListener.onSlidingClosed(panel);
    }

    @Override
    public void setSlidingEnable(int type, boolean ifEnable) {
        mySliddingMenu.setSlidingMenuEnable(ifEnable);
    }
}
